package com.hava.contentsale.web.controller;

import com.hava.contentsale.meta.ProductVO;
import com.hava.contentsale.meta.UserVO;
import com.hava.contentsale.service.ProductService;
import com.hava.contentsale.web.session.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by zhanpeng on 2016/11/11.
 * R1 展示（首页）
 */
@Controller
public class IndexController {

    @Autowired
    ProductService productServiceImpl;

    @RequestMapping(value = "/")
    public ModelAndView index_view(HttpSession httpSession)
    {
        System.out.println("Class IndexController Method index_view");
        //session
        System.out.println("[session.id]:" + httpSession.getId());
        UserVO userVO = (UserVO) httpSession.getAttribute("user");

        ModelAndView modelAndView = new ModelAndView("index");
        //当用户不为空时，返回用户信息
        if(userVO != null)
            modelAndView.addObject("user", userVO);

        List<ProductVO> productVOs = this.productServiceImpl.findAll(userVO);
        for(ProductVO productVO : productVOs)
            productVO.printProperties();

        modelAndView.addObject("productList", productVOs);
        return modelAndView;
    }
}
